package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.core.annotation.Validator;
import com.sixmac.core.base.model.Format;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.grid.BladePage;
import com.sixmac.core.toolbox.kit.StrKit;
import com.sixmac.core.toolbox.support.Convert;
import com.sixmac.model.*;
import com.sixmac.service.IndexService;
import com.sixmac.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * Created by admin on 2018/7/11.
 */
@Controller
@RequestMapping("/api/record")
public class RecordController extends ApiController {
    @Autowired
    IndexService indexService;
    @Autowired
    RecordService recordService;

    /**
     * @api {post} /api/record/index   01.学习记录
     * @apiName record.login
     * @apiGroup record
     * @apiParam {String}  token  token
     * @apiParam {String}  date  date比如(2018-07)
     * @apiParam {String}  page  当前页 默认为1
     * @apiParam {String}  size  每页大小 默认为6
     * @apiSuccess {String} data.rows.id 课程id
     * @apiSuccess {String} data.rows.image 封面图片
     * @apiSuccess {String} data.rows.point 学习要点&考点(富文本)
     * @apiSuccess {String} data.rows.s 百分比
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/index")
    public AjaxResult index(String date, ClienUser user, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        BladePage<ClientUserChapterRecord> recordList = Blade.create(ClientUserChapterRecord.class).paginate("select * from t_user_chapter_record where userId = #{userId} and date_format(createTime,'%Y-%m')  = #{date}  group by courseId ", getCMap().put("userId", user.getId()).put("date", date), page, size);
        List<Map> list = new ArrayList<>();
        for (ClientUserChapterRecord record : recordList.getRows()) {
            Map map = new HashMap();
            String s = indexService.calus(record.getCourseId(), user.getId());
            CourseChapter course = Blade.create(CourseChapter.class).findById(record.getCourseId());
            Map m = Db.selectOne("select id,name,content as point,image,url,pid,createtime,updatetime,level from t_course_chapter where id=" + course.getCourseId());
            map.put("s", s);
            map.put("course", m);
            list.add(map);
        }
        return json(list);
    }

    /**
     * @api {post} /api/record/task   02.我的任务
     * @apiName record.task
     * @apiGroup record
     * @apiParam {String}  token  token
     * @apiParam {String}  page  当前页 默认为1
     * @apiParam {String}  size  每页大小 默认为6
     * @apiSuccess {String} data.rows.id  任务id
     * @apiSuccess {String} data.rows.image 封面图片
     * @apiSuccess {String} data.rows.name 任务名称
     * @apiSuccess {String} data.rows.s 百分比
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/task")
    public AjaxResult task(ClienUser user, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        if (StrKit.isEmpty(user.getGrade())) {
            return error("用户没有分配班级");
        }
        BladePage<Map> bladePage = Db.paginate("select cc.* from t_class_chapter cc  left join t_user_class_chapter uc on uc.chapterId = cc.id where cc.pid =0 and uc.classId =#{classId}", getCMap().put("classId", user.getGrade()), page, size);
        for (Map m : bladePage.getRows()) {
            m.put("s", recordService.calu(user.getId(), Convert.toInt(m.get("id"))));
        }
        return json(bladePage);
    }

    /**
     * @api {post} /api/record/taskIndex   03.任务目录
     * @apiName record.taskIndex
     * @apiDescription 三级目录
     * @apiGroup record
     * @apiParam {String}  token  token
     * @apiParam {String}  id  id 课程id
     * @apiSuccess {String} data.rows.name  任务名称
     * @apiSuccess {String} data.rows.other  1未完成2已完成
     * @apiSuccess {String} data.rows.name 任务名称 List里面包含的name
     * @apiSuccess {String} data.rows.id 任务章节id
     * @apiSuccess {String} data.rows.status 1未完成2已完成
     * @apiSuccess {String} data.rows.type 1（通过上面的任务章节id调用04）视频 2模拟训练（不管这个摆设）
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/taskIndex")
    public AjaxResult taskIndex(ClienUser user, Integer id) {
        return recordService.taskIndex(id, user);
    }

    /**
     * @api {post} /api/record/getIndexById   04.通过任务id进入知识点
     * @apiName record.getIndexById
     * @apiGroup record
     * @apiParam {String}  token  token
     * @apiParam {String}  id   任务章节id
     * @apiSuccess {String} data.rows.name  任务名称
     * @apiSuccess {String} data.rows.image 任务图片
     * @apiSuccess {String} data.rows.url 视频地址
     * @apiSuccess {String} data.rows.content 富文本 知识点简介
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/getIndexById")
    @Validator(name = {"id"}, format = {Format.NOT_NULL})
    public AjaxResult getIndexById(ClienUser user, Integer id) {
        ClientClassChapter classChapter = Blade.create(ClientClassChapter.class).findById(id);
        recordService.record(user, id, 1);
        return json(classChapter);
    }

    /**
     * @api {post} /api/record/showComment   05.显示评论
     * @apiName record.showComment
     * @apiGroup record
     * @apiParam {String}  token  token
     * @apiParam {String}  id  任务章节id
     * @apiParam {String}  page  当前页 默认为1
     * @apiParam {String}  size  每页大小 默认为6
     * @apiSuccess {String} data.rows.content 评论内容
     * @apiSuccess {String} data.rows.createtime 创建时间
     * @apiSuccess {String} data.rows.name 评论名字
     * @apiSuccess {String} data.rows.headimg 头像
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/showComment")
    @Validator(name = {"id"}, format = {Format.NOT_NULL})
    public AjaxResult showComment(ClienUser user, Integer id, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        return json(Db.paginate("select c.*,u.name,u.headimg from t_class_chapter_comment c   left join t_user u on u.id = c.userId  where chapterId =  #{id} ", getCMap().put("id", id), page, size));
    }

    /**
     * @api {post}  /api/record/editComment   06.编写评论
     * @apiName record.editComment
     * @apiGroup record
     * @apiParam {String}  token  token
     * @apiParam {String}  id  任务章节id
     * @apiParam {String} content 用户评论内容
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/editComment")
    @Validator(name = {"id", "content"}, format = {Format.NOT_NULL, Format.NOT_NULL})
    public AjaxResult editComment(ClienUser user, Integer id, String content) {
        ClientClassChapterComment comment = new ClientClassChapterComment();
        comment.setUserId(user.getId());
        comment.setChapterId(id);
        comment.setCreatetime(new Date());
        comment.setContent(content);
        boolean f = Blade.create(ClientClassChapterComment.class).save(comment);
        if (f) {
            return success("保存成功");
        } else {
            return error("保存失败");
        }

    }

    /**
     * @api {post}  /api/record/finsh   看完我的任务的视频调用这个接口
     * @apiName record.finsh
     * @apiDescription 这个接口没有返回值
     * @apiGroup record
     * @apiParam {String}  token  token
     * @apiParam {String}  id  任务章节id
     * @apiParam {String} content 用户评论内容
     * @apiError {Integer} 1
     */
    @AuthUser(isGuess = false)
    @RequestMapping("/finsh")
    public void finsh(ClienUser user, Integer id) {
        recordService.record(user, id, 2);
    }


}
